<?php

/**
 * 夏日葵电商系统
 * http://www.xiarikui.com
 */

namespace Admin\Controller;
use Think\AjaxPage;

class OrderController extends BaseController {

    public function index(){
        auatomatic();
        $mod = M('orders');
        $where = array('is_delete'=>0,'is_child'=>0);
        $p = empty($_REQUEST['p']) ? 1 : $_REQUEST['p'];
        $size = 20;
        $count = $mod->where($where)->count();
        $pager = new \Think\Page($count, $size);
        $page = $pager->show();
        $goods_mod = M('orders_goods');
        $order_list = $mod->where($where)->order('add_time desc')->page("$p,$size")->select();
        foreach((array)$order_list as $k=>$v){
            $order_list[$k]['goods_list'] = $goods_mod->alias('as a')
                ->join('left join '.C('DB_PREFIX').'goods as c on c.id=a.goods_id')
                ->join('left join '.C('DB_PREFIX').'goods_store_spec as b on b.id=a.spec_id')
                ->join('left join '.C('DB_PREFIX').'business as d on d.id=a.store_id')
                ->where(array('a.order_sn'=>$v['order_sn']))
                ->field('c.goods_name,c.thumb,a.goods_number,a.goods_price,b.spec_name,d.store_name')
                ->select();
        }
        $this->assign('order_list',$order_list);
        $this->assign('page', $page);
        $this->display();
    }
    public function detail(){
        $id = I('id')?I('id'):'';
        if($id){
            $mod = M('orders');
            $goods_mod = M('orders_goods');
            $info = $mod->where(array('id'=>$id))->find();
            $orderGoods = $goods_mod->alias('as a')
                ->join('left join '.C('DB_PREFIX').'goods as b on b.id=a.goods_id')
                ->join('left join '.C('DB_PREFIX').'goods_store_spec as c on c.id=a.spec_id')
                ->field('b.goods_name,c.spec_name,a.goods_number,a.goods_price')
                ->where(array('order_sn'=>$info['order_sn']))
                ->select();
            foreach((array)$orderGoods as $k=>$v){
                $info['goods_price']+=$v['goods_price']*$v['goods_number'];
            }
            $this->assign('orderGoods',$orderGoods);
            $this->assign('order',$info);
            $orders_status = M('orders_status')->where(array('order_sn'=>$info['order_sn']))->select();
            $this->assign('action_log',$orders_status);
            $this->display();
        }
    }
    //发货
    public function deliveryInfo(){
        $mod = M('orders');
        $id = I('order_id')?I('order_id'):'';
        $admin_id = $_SESSION['admin_id'];
        $info = $mod->where(array('id'=>$id))->field('status,order_sn,order_status')->find();
        if($info['status']<2){
            $this->error('该订单未完成支付');
        }
        if($info['order_status']>0){
            $this->error('该订单已发货');
        }
        $result = $mod->where(array('id'=>$id))->save(array('order_status'=>1,'ship_time'=>time()));
        if($result){
            $user_name = M('admin')->where(array('id'=>$_SESSION['admin_id']))->getField('user_name');
            orders_status($user_name,$info['order_sn'],2,1,I('resume'));
            order_log($info['order_sn'],'待收货','商品正在配送中');
            exit(json_encode(array('code'=>200,'msg'=>'发货成功')));
        }
    }
}
